package org.mybatis.mapper.impl;

import org.apache.ibatis.reflection.ParamNameResolver;
import org.apache.ibatis.session.SqlSession;
import org.common.entity.Employee;
import org.mybatis.mapper.IEmployeeDAO;
import org.mybatis.util.MyBatisUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Employee实现类: 通过Mybatis来实现的版本。
 *
 * @author yexi
 * @see ParamNameResolver
 */
public class EmployeeDAOImpl implements IEmployeeDAO {

    private final String PREFIX;

    public EmployeeDAOImpl(String prefix) {
        PREFIX = prefix;
    }

    @Override
    public int insert(Employee employee) {
        //工具类的抽取不再说明，请自行阅读
        SqlSession session = MyBatisUtil.openSession();
        int rows = session.insert(PREFIX + "insert", employee);
        session.commit();//提交事务
        session.close();
        return rows;
    }

    @Override
    public int deleteById(Long aLong) {
        SqlSession session = MyBatisUtil.openSession();
        int rows = session.delete(PREFIX + "deleteById", aLong);
        session.commit();
        session.close();
        return rows;
    }

    @Override
    public int updateById(Employee employee) {
        SqlSession session = MyBatisUtil.openSession();
        int rows = session.update(PREFIX + "updateById", employee);
        session.commit();
        session.close();
        return rows;
    }

    @Override
    public Employee selectById(Long id) {
        SqlSession session = MyBatisUtil.openSession();
        Employee employee = session.selectOne(PREFIX + "selectById", id);
        session.close();
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        SqlSession session = MyBatisUtil.openSession();
        List<Employee> employees = session.selectList(PREFIX + "selectAll");
        session.close();
        return employees;
    }

    @Override
    public int selectCount() {
        SqlSession session = MyBatisUtil.openSession();
        int count = session.selectOne(PREFIX + "selectCount");
        session.close();
        return count;
    }

    @Override
    public List<Employee> selectByIds(List<Integer> ids) {
        SqlSession session = MyBatisUtil.openSession();
        List<Employee> employees = session.selectList(PREFIX + "selectByIds", ids);
        session.close();
        return employees;
    }

    @Override
    public List<Employee> selectBySelective(Employee employee) {
        SqlSession session = MyBatisUtil.openSession();
        List<Employee> employees = session.selectList(PREFIX + "selectBySelective", employee);
        session.close();
        return employees;
    }

    @Override
    public List<Employee> selectHighSalary(int amount) {
        SqlSession session = MyBatisUtil.openSession();
        List<Employee> employees = session.selectList(PREFIX + "selectHighSalary", amount);
        session.close();
        return employees;
    }

    @Override
    public List<Employee> selectPage(int pageSize, int pageNo) {
        SqlSession session = MyBatisUtil.openSession();
        Map<String, Integer> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNo", pageNo);
        List<Employee> employees = session.selectList(PREFIX + "selectPage", map);
        session.close();
        return employees;
    }

    @Override
    public int insertSelective(Employee employee) {
        SqlSession session = MyBatisUtil.openSession();
        int rows = session.insert(PREFIX + "insertSelective", employee);
        session.commit();//提交事务
        session.close();
        return rows;
    }

    @Override
    public int updateByIdSelective(Employee employee) {
        SqlSession session = MyBatisUtil.openSession();
        int rows = session.update(PREFIX + "updateByIdSelective", employee);
        session.commit();
        session.close();
        return rows;
    }
}
